package com.qq.spirit.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;

import com.qq.spirit.CryptUtil;
import com.qq.spirit.GameUserInfoUtil;
import com.qq.spirit.GetUserInfoTask;
import com.qq.spirit.HttpUtil;
import com.qq.spirit.TaskManager;
import com.qq.spirit.UserState;
import com.qq.spirit.model.UserData;

public class YaowanLogin implements LoginInter
{
	@Override
	public void doLogin(int gid)
	{
		 final UserData u = GameUserInfoUtil.getUser(gid);
		// 先查一下是否能够获取个人信息，如果可以，则认为可以登录，否则，触发登录
		// 登录网站吧
		List<BasicNameValuePair> pList = new ArrayList<BasicNameValuePair>();
		pList.add(new BasicNameValuePair("tourl", "http://www.yaowan.com/?m=user"));
		pList.add(new BasicNameValuePair("entergame", ""));
		pList.add(new BasicNameValuePair("districtid", ""));
		pList.add(new BasicNameValuePair("username", u.getGameInfo("game_user")));
		pList.add(new BasicNameValuePair("password", CryptUtil.decode(u.getGameInfo("game_password"))));
		pList.add(new BasicNameValuePair("button", "登录"));

		HttpUtil.doPost(u.getGid(), "http://www.yaowan.com/?m=user&action=loginform", pList, new DefaultRedirectStrategy() {
			@Override
			public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException
			{
				Header[] headerList = response.getHeaders("Set-Cookie");
				if (headerList != null)
				{
					for (Header h : headerList)
					{
						HeaderElement[] hs = h.getElements();
						if (hs != null && hs.length > 0)
						{
							for (HeaderElement he : hs)
							{
								if (he.getName().equals("PHPSESSID") || he.getName().equals("usersign") || he.getName().equals("userid"))
								{
									u.putCookie(he.getName(), he.getValue());
									break;
								}
							}
						}
					}
				}
				Header h = response.getFirstHeader("Location");
				if (h != null)
				{
					if (h.getValue().equals("http://www.yaowan.com/?m=user"))
					{
						// 登录成功
						u.setUserState(UserState.LOGIN_WEB);
					}
				}
				return false;
			}

		}, null);

		// 已经登录了网站，需要构建JSESSIONID
		if (u.getUserState() == UserState.LOGIN_WEB)
		{
			// 生成JSESSIONID
			HttpUtil.doGet(gid, "http://www.yaowan.com/?m=game&game_id=15&district_id=198&isEnteredGame=1", new DefaultRedirectStrategy() {
				public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException
				{
					// 判断是否有产生游戏sessionId
					Header[] headerList = response.getHeaders("Set-Cookie");
					if (headerList != null)
					{
						for (Header h : headerList)
						{
							HeaderElement[] hs = h.getElements();
							if (hs != null && hs.length > 0)
							{
								for (HeaderElement he : hs)
								{
									if (he.getName().equals("JSESSIONID"))
									{
										u.putCookie(he.getName(), he.getValue());
										u.setUserState(UserState.LOGIN_GAME);
										break;
									}
								}
							}
						}
					}
					return super.isRedirected(request, response, context);
				}

				@Override
				public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException
				{
					Header h = response.getFirstHeader("Location");
					if (h != null)
					{
						Header[] h2 = request.getAllHeaders();
						HttpGet hg = new HttpGet(h.getValue());
						if (h2 != null)
						{
							for (Header h3 : h2)
							{
								hg.setHeader(h3);
							}
						}
						return hg;
					}
					return null;
				}
			}, null);
		}
	}
}
